// pages/write_plan/write_plan.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        title:'',
        content:'',
        planStartTime: '',
        planEndTime: '',
        date: '',
        planStartTimeShow: false,
        planEndTimeShow: false,
        planStartTimeAfter:'',
        planEndTimeAfter:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    onStartDisplay() {
        this.setData({ planStartTimeShow: true });
    },
    onEndDisplay() {
        this.setData({ planEndTimeShow: true });
    },
    onStartClose() {
        this.setData({ planStartTimeShow: false });
    },
    onEndClose() {
        this.setData({ planEndTimeShow: false });
    },

    formatDate(date) {
        date = new Date(date);
        return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    onStartConfirm(event) {
        console.log(event.detail)
        this.setData({
            planStartTimeAfter: event.detail,
            planStartTimeShow: false,
            planStartTime: this.formatDate(event.detail),
        });
    },

    onEndConfirm(event) {
        this.setData({
            planEndTimeAfter: event.detail,
            planEndTimeShow: false,
            planEndTime: this.formatDate(event.detail),
        });
    },

    onInput(event) {
        this.setData({
            currentDate: event.detail,
        });
    },

    titleInput(e){
        this.setData({
            title: e.detail
        })
    },
    contentInput(e){
      this.setData({
          content: e.detail
      })
    },
    writePlan(){
        wx.request({
            url: app.globalData.baseUrl + '/plan/planCreate',
            method: 'POST',
            header: {
                "Content-Type": "application/json;charset=utf-8",
                "token": wx.getStorageSync('token')
            },
            data: {
                title: this.data.title,
                content: this.data.content,
                planStartTime: this.data.planStartTimeAfter,
                planEndTime: this.data.planEndTimeAfter
            },
            success: res => {
                if (res.data.code === 200) {
                    wx.showToast({
                        title: '发布成功',
                        icon: 'none'
                    });
                    wx.switchTab({
                        url: '../agency/agency'
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    });
                }
            }
        })
    },
})
